/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.teama;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static java.util.logging.Level.INFO;

/**
 *
 * @author bamidei
 */
public class IssueDA {

    static public boolean insert(Issue is) {
       boolean retVal;
       String queryText;
       
       if(is.getIssueId() == -1)
       {
           /*This is a new insert */
            queryText = "INSERT INTO BugTracker.defects " +
                              "(app_no, summary, descrp, priority_no, submitter, created, assignee, status_no)" +
                              " VALUES ('" + 
                              is.getAppId() + "', '" + 
                              is.getIssueSummary().replace("'", "\\'") + "', '" + is.getIssueDesc().replace("'", "\\'") + "', '" +
                              is.getPriorityId() + "', '" + is.getSubmitById() + "', '" +      
                              is.getCreatedDate() + "', '" + is.getAssignToId() + "', '" + 
                              is.getStatusId() + "');";
           
       }
       else{
           /*This is an update */
           queryText = "UPDATE BugTracker.defects SET " +
                       "app_no='" + is.getAppId() + "', summary='" + is.getIssueSummary().replace("'", "\\'") +
                       "', descrp='" + is.getIssueDesc().replace("'", "\\'") + "', priority_no='" + is.getPriorityId() + 
                       "', submitter='" + is.getSubmitById() + "', created='" + is.getCreatedDate() + 
                       "', assignee='" + is.getAssignToId() + "', status_no='" + is.getStatusId() +
                       "' WHERE request_id='" + is.getIssueId() + "';";           
       }
       try {
            ResultSet rs;
            Connection conn = DBconnect.getConnection();
            Statement statement = conn.createStatement();
            
            statement.execute (queryText);
            statement.close();
            conn.close();
            retVal = true;
        } catch (SQLException e) {
            DBconnect.getLogger().log(INFO, "Database Error inserting issue info.", e);
            retVal=false;
        }       
       return retVal;    
    }    
}
